package com.creataion.prototype;

import java.util.HashMap;
import java.util.Map;

public class TaoBao {
    //    缓存戒指，序列化和反序列化-深克隆
    private Map<String, DrRing> drRingCache = new HashMap<>();

    //    从缓存中获取戒指
    public DrRing getDrRing(String name) {
        DrRing drRing = null;
//        如果此映射包含指定键的映射，则返回 true 。
//        drRingCache缓存中没有
        if (!drRingCache.containsKey(name)) {
//            ctrl+shift+上键
//            ctrl+alt+l 代码格式化
            drRing = getDrRingFromDb(name);
        }else {
            drRing = drRingCache.get(name);
            System.out.println("从缓存中拿到了是："+drRing);
        }
        return drRing;
    }

    private DrRing getDrRingFromDb(String name) {
        System.out.println("从数据库查到" + name);
        DrRing drRing = new DrRing();
        drRing.setName("黄金小猪佩奇");
        drRing.setPrice(5000f);
//        给缓存中放一个clone
        drRingCache.put(name, (DrRing) drRing.clone());
        return drRing;
    }
}
